package com.kbt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Toast;
import com.android.volley.Request;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.kbt.activity.*;
import com.kbt.adapter.GoodInfoAdapter;
import com.kbt.adapter.NewsAdapter;
import com.kbt.commont.Constants;
import com.kbt.model.data.AddressListBean;
import com.kbt.model.data.NewsBean;
import com.kbt.model.data.NewsListBean;
import com.kbt.net.RequestManager;
import com.kbt.util.utils.LoadingDialog;
import com.kbt.util.utils.NoScrollListView;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;

import javax.sql.RowSetListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻
 * Created by kuajie on 15/7/31.
 */
public class NewsFragment extends Fragment implements RequestManager.ResponseInterface{

    private SharePreferenceUtils share;
    private View view;
    private NoScrollListView newsListView;
    private NewsAdapter newsAdapter;
    private List<NewsBean> newsBeanList = new ArrayList<NewsBean>();
    private PullToRefreshScrollView newsPullScrollView;
    private Boolean isRefreshing = false;
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private LoadingDialog mProgress;
    private NewsListBean newsListBean;
    private List<NewsBean> newsBeans = new ArrayList<NewsBean>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.news, container, false);
//        initData();
        init();
        //获取新闻数据
        getNewsData();
        setListener();
        return view;
    }

    // 初始化
    private void init(){
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        share = new SharePreferenceUtils(getActivity());
        newsListView = (NoScrollListView) view.findViewById(R.id.newsList);
        newsPullScrollView = (PullToRefreshScrollView) view.findViewById(R.id.newsPullScrollView);
        newsPullScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//设置下拉
        newsAdapter = new NewsAdapter(getActivity(),newsBeanList);
        newsListView.setAdapter(newsAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
    //监听
    private void setListener(){
        newsPullScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (!isRefreshing) {
                    isRefreshing = true;
                    if (refreshView.isFooterShown()) {
                        refresh(true);
                    } else if (refreshView.isHeaderShown()) {
                        refresh(false);
                    }
                } else {
                    refreshView.onRefreshComplete();
                }
            }
        });
        newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("html",newsBeans.get(position).getNews_url());
                intent.putExtra("title",newsBeans.get(position).getNews_title());
                startActivity(intent);
            }
        });

    }
    //刷新
    protected void refresh(Boolean isNextPage) {
        if(isNextPage) {

        }else{
            getNewsData();
//            if(newsAdapter==null) {
//                newsAdapter = new NewsAdapter(getActivity(),newsBeanList);
//                newsListView.setAdapter(newsAdapter);
//            }else{
//                newsAdapter.setList(newsBeanList);
//                newsAdapter.notifyDataSetChanged();
//            }
            isRefreshing=false;
            newsPullScrollView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    newsPullScrollView.onRefreshComplete();
                }
            }, 1000);
        }
    }
    //获取新闻数据
    private void getNewsData(){
        showProgress();
        if(Utils.isNetworkAvailable(getActivity())) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectNewsUrl, Constants.RequestTag.selectNewsTag, map, NewsListBean.class);
        }else{
            stopProgress();
            Toast.makeText(getActivity(),R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * progressDialog的展示
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new LoadingDialog(getActivity(),"");
            mProgress.setCancelable(true);
            mProgress.show();

        } else {
            if (mProgress.isShowing()) {
                stopProgress();
            } else {
                mProgress.show();
            }
        }
    }
    /**
     * progressDialog的消失
     */
    public void stopProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        stopProgress();
        if (tag == Constants.RequestTag.selectNewsTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                newsListBean = (NewsListBean) parsedGSON;
                if (newsListBean.getResult().equals("success")) {
                    if(newsListBean.getData().size()>0){
                        newsBeans =newsListBean.getData();
                        newsAdapter.setList(newsBeans);
                        newsAdapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        stopProgress();
        Toast.makeText(getActivity(),R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
}
